﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace STKMIS
{
    /// <summary> 
    /// 声明委托
    /// </summary> 
    /// <param name="e"></param> 
    public delegate int EventPaginationHandler(EventPaginationArg e);

    public partial class Pagination : UserControl
    {
        public Pagination()
        {
            InitializeComponent();
        }
        public event EventPaginationHandler EventPagination;
        #region 成员变量
        private int _rowsPerPage=30;
        private int _currentPage=1;
        private int _pageCount=0;
        private int _rowsCount=0;
        #endregion
        #region 属性


        /// <summary>
        /// 获取或设置每页行数。
        /// </summary>
         
        public int RowsPerPage
        {
            get
            {
                return _rowsPerPage;
            }
            set
            {
                _rowsPerPage = value;
                GetPageCount();
                txtRowsPerPage.Text = _rowsPerPage.ToString();
            }
        }

        /// <summary>
        /// 获取或设置当前页序号。
        /// </summary>
        
        public int CurrentPage
        {
            get
            {
                return _currentPage;
            }
            set
            {
                _currentPage = value;
                lblCurrentPage.Text = _currentPage.ToString();
                //if (_pageCount != 0 && value > _pageCount)
                //{
                //    throw new ArgumentOutOfRangeException("CurrentPage", "设置的数据页不存在。");
                //}
                //else
                //{
                //    _currentPage = value;
                //    txtCurrentPage.Text = _currentPage.ToString();
                //    lblCurrentPage.Text = _currentPage.ToString();
                //}
            }
        }

        /// <summary>
        /// 获取或设置总页数。
        /// </summary>
        public int PageCount
        {
            get
            {
                return _pageCount;
            }
            set
            {
                _pageCount = value;
               
                lblPageCount.Text = _pageCount.ToString();
            }
        }
        /// <summary>
        /// 获取或设置数据总行数。
        /// </summary>
        /// 
        public int RowsCount
        {
            get
            {
                return _rowsCount;
            }
            set
            {
                _rowsCount = value;
                GetPageCount();
                lblRowCount.Text = _rowsCount.ToString();
            }
        }

        #endregion
        #region 方法
        /// <summary>
        /// 将指定的数字字符串转换为整型类型，如果指定字符串不能转换成整型则返回默认值。
        /// </summary>
        /// <param name="s">需要被转换字符串。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>转换后整型值。</returns>
        private int ToInt(string s, int defaultValue)
        {
            try
            {
                int n = Convert.ToInt32(s);
                if (n <= 0)
                {
                    n = defaultValue;
                }
                return n;
            }
            catch
            {
                return defaultValue;
            }
        }

        private void GetPageCount()
        {
            if (this.RowsCount > 0)
            {
                this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.RowsCount) / Convert.ToDouble(this.RowsPerPage)));
            }
            else
            {
                this.PageCount = 0;
            }
        }

        /// <summary>
        /// 提供外部绑定
        /// </summary>
        /// 
        public void BindGridView()
        {
            if (this.EventPagination != null)
            {
                this.RowsCount = this.EventPagination(new EventPaginationArg(this.CurrentPage)); 
            }

            if (this.CurrentPage > this.PageCount)
            {
                this.CurrentPage = this.PageCount;
            }

            this.txtCurrentPage.Text = this.CurrentPage.ToString();
            this.lblCurrentPage.Text = this.CurrentPage.ToString();
            this.lblRowCount.Text = this.RowsCount.ToString();
            this.lblPageCount.Text = this.PageCount.ToString();

            if (this.RowsCount > 0)
            { 
                if (_currentPage != 1)
                {
                    this.btnFirst.Enabled = true;
                    this.btnPrev.Enabled = true;
                    this.btnFirst.Image = global::STKMIS.Properties.Resources.btn_pgnFirst;
                    this.btnPrev.Image = global::STKMIS.Properties.Resources.btn_pgnPrevious;
                }
                else
                {
                    this.btnFirst.Enabled = false;
                    this.btnPrev.Enabled = false;
                    this.btnFirst.Image = global::STKMIS.Properties.Resources.btn_pgnFirst2;
                    this.btnPrev.Image = global::STKMIS.Properties.Resources.btn_pgnPrevious2;
                }
                if (_currentPage != _pageCount)
                {
                    this.btnNext.Enabled = true;
                    this.btnLast.Enabled = true;
                    this.btnNext.Image = global::STKMIS.Properties.Resources.btn_pgnNext;
                    this.btnLast.Image = global::STKMIS.Properties.Resources.btn_pgnLast;
                }
                else
                {
                    this.btnNext.Enabled = false;
                    this.btnLast.Enabled = false;
                    this.btnNext.Image = global::STKMIS.Properties.Resources.btn_pgnNext2;
                    this.btnLast.Image = global::STKMIS.Properties.Resources.btn_pgnLast2;
                }
            }
            else if (this.RowsCount == 0)
            {
                CurrentPage = 1;
                this.btnFirst.Enabled = false;
                this.btnPrev.Enabled = false;
                this.btnNext.Enabled = false;
                this.btnLast.Enabled = false;
                this.btnGo.Enabled = false;
                this.btnFirst.Image = global::STKMIS.Properties.Resources.btn_pgnFirst2;
                this.btnPrev.Image = global::STKMIS.Properties.Resources.btn_pgnPrevious2;
                this.btnNext.Image = global::STKMIS.Properties.Resources.btn_pgnNext2;
                this.btnLast.Image = global::STKMIS.Properties.Resources.btn_pgnLast2;
            }

        }
        #endregion
        #region 事件
        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                CurrentPage = 1;
                BindGridView();
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                CurrentPage -= 1;
                BindGridView();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (_currentPage < _pageCount)
            {
                CurrentPage += 1;
                if (CurrentPage == PageCount)
                {
                    btnNext.Enabled = false;
                }
                BindGridView();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (_currentPage < _pageCount)
            {
                CurrentPage = _pageCount;
                BindGridView();
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            BindGridView();
        }
        private void txtRowsPerPage_TextChanged(object sender, EventArgs e)
        {
            _rowsPerPage = ToInt(txtRowsPerPage.Text, _rowsPerPage);
            // 每页行数改变后，当前页必须回到第一页，否则可能会导致指定的页不存在的问题。
            CurrentPage = 1;
        }

        private void txtCurrentPage_TextChanged(object sender, EventArgs e)
        {
            _currentPage = ToInt(txtCurrentPage.Text, _pageCount);

            // 判断指定的页是否存在
            if (_currentPage > _pageCount)
            {
                CurrentPage = _pageCount;
                BindGridView();
            }
        }
        #endregion
 
    }

    
    /// <summary>
    /// 自定义事件数据基类
    /// </summary>
    public class EventPaginationArg : EventArgs
    {
        private int _intPageIndex;
        public EventPaginationArg(int PageIndex)
        {
            _intPageIndex = PageIndex;
        }
    }

}
